#!/bin/bash

# JayVii
# https://notabug.org/jayvii
# musicsorter v2.0

# counting files (ignoring folders)
MANY_TRACK=$(find $1 -type f | wc -l)
ALREADY_EXIST=0

# start sorting
while (($MANY_TRACK > 0))
do

	# choosing next file
	CURRENT_TRACK=$(find $1 -type f | sed -n "${MANY_TRACK}","${MANY_TRACK}"p)
	
	# assigning variables
	TITLE=$(mediainfo --Inform="General;%Title%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")                                                  
	ARTIST=$(mediainfo --Inform="General;%Album/Performer%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")                                       
	TRACKARTIST=$(mediainfo --Inform="General;%Performer%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")                                        
	ALBUM=$(mediainfo --Inform="General;%Album%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")                                                  
	YEAR_ORIG=$(mediainfo --Inform="General;%Original/Released_Date%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")                             
	YEAR=$(mediainfo --Inform="General;%Released_Date%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")                                           
	YEAR_RECORD=$(mediainfo --Inform="General;%Recorded_Date%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")                                    
	TRACKNUM=$(mediainfo --Inform="General;%Track/Position%" "$CURRENT_TRACK" | tr "/" "_" | tr "'" "_")                                      
	FILESUFFIX=$(echo $CURRENT_TRACK | awk -F . '{print $NF}')                                                                                
	#FILENAME=$($1/${CURRENT_TRACK%.*}) 
	

	# checking if variables are set
	if [ -z "$TITLE" ]; then TITLE="${MANY_TRACK}"; fi
	if [ -z "$TRACKARTIST" ]; then TRACKARTIST="unknown_artist"; fi
	if [ -z "$ARTIST" ]; then ARTIST="${TRACKARTIST}"; fi
	if [ -z "$ALBUM" ]; then ALBUM="unknown_album"; fi
	if [ -z "$YEAR_ORIG" ]; then YEAR_ORIG="0000"; fi
	if [ -z "$YEAR_RECORD" ]; then YEAR_RECORD="${YEAR_ORIG}"; fi
	if [ -z "$YEAR" ]; then YEAR="${YEAR_RECORD}"; fi
	if [ -z "$TRACKNUM" ]; then TRACKNUM="0"; fi

	# control output
	echo "################################"
	echo "[$MANY_TRACK]"
	echo "Source: $CURRENT_TRACK"
	echo "Target: ${2}/${ARTIST}/${ALBUM}/${TRACKNUM} - ${TITLE}.${FILESUFFIX}"
	echo " "

	# creating folder if necessary
	mkdir -p "${2}/${ARTIST}/${ALBUM}/"
	
	IF_EXIST=$(ls -1 "${2}/${ARTIST}/${ALBUM} (${YEAR})/${TRACKNUM} - ${TITLE}.*" 2>/dev/null | wc -l)
	if [ $IF_EXIST != 0 ]
	then
		# if file exists, move new one into the "DOUBLE_EXISTANCE" folder
		echo "Already exists!"
		ALREADY_EXIST=$(($ALREADY_EXIST + 1))
		mkdir -p "${2}/DOUBLE_EXISTANCE/"
		mv "${CURRENT_TRACK}" "$2/DOUBLE_EXISTANCE/${TRACKNUM} - ${TITLE}.${FILESUFFIX}"
	else
		# moving file into corresponding folder
		mv "${CURRENT_TRACK}" "$2/${ARTIST}/${ALBUM}/${TRACKNUM} - ${TITLE}.${FILESUFFIX}"
	fi
MANY_TRACK=$((${MANY_TRACK} - 1))

done

echo " "
echo "################################"
echo "I am done!"
echo "${ALREADY_EXIST} files already existed and were moved into ${2}/DOUBLE_EXISTANCE/"
echo " "
echo "Checking if Sourcefolder is empty... (if no output in 20 seconds, it is empty)"
sleep 20
find "$1" -type f
